<style>
    .btn-red{
        background-color: #F92659;
    }
</style>
<?php
use yii\widgets\LinkPager;
?>
<section id="main-content">
    <section class="wrapper">
    <!-- page start-->
        <div class="row">
            <div class="col-lg-12">
                <section class="panel panel-info">
                    <header class="panel-heading">
                        帐号管理
                    </header>
                    <div class="panel-body">
                        <form class="navbar-form form-inline" role="search" method="post" target="_self">
                            <div class="row m-b">
                                <div class="form-group">
                                    <label class=" control-label">帐号</label>
                                    <input type="text" id="search_name" class="form-control" value="<?php echo !empty($data['username'])?$data['username']:'';?>">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">手机号码</label>
                                    <input type="text" id="search_mobile" class="form-control" value="<?php echo !empty($data['mobile'])?$data['mobile']:'';?>">
                                </div>
                                <button type="button" class="btn  btn-primary slecctform">搜索</button>
                                <?php if(Yii::$app->cache->get('admin')['role'] != 3){?>
                                    <a class="btn btn-primary" data-toggle="modal" data-target="#myModal1">新建用户</a>
                                <?php }?>
                            </div>
                        </form>
                    </div>
                    <ul class="list-group list-area">
                        <li class= 'list-group-item'>
                            <span style="width: 5%;float: left;display: inline-block;">ID</span>
                            <span style="width: 10%;float: left;display: inline-block;padding:0 5px">帐号</span>
                            <span style="width: 15%;float: left;display: inline-block;padding:0 5px">手机号码</span>
                            <span style="width: 10%;float: left;display: inline-block;padding:0 5px">创建者</span>
                            <span style="width: 15%;float: left;display: inline-block;padding:0 5px">角色</span>
                            <span style="width: 5%;float: left;display: inline-block;padding:0 5px">状态</span>
                            <span style="width: 20%;float: left;display: inline-block;padding:0 5px">创建时间</span>
                            <span style="width: 20%;float: right;display: inline-block;padding:0 5px">操作</span>
                        </li>
                        <?php if(!empty($model)){foreach($model as $key => $value){?>
                        <li class= 'list-group-item  <?php echo $value['status'] == 1 ? "label-success" :''?>'>
                            <span style="width: 5%;float: left;display: inline-block;"><?php echo !empty($value['id']) ? $value['id'] :'';?></span>
                            <span style="width: 10%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['username']) ? $value['username'] :'';?></span>
                            <span style="width: 15%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['mobile']) ? $value['mobile'] :'';?></span>
                            <span style="width: 10%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['creator']) ? $value['creator'] :'';?></span>
                            <span style="width: 15%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['role']) ? $value['role'] :'';?></span>
                            <span style="width: 5%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['status']) && $value['status'] == 1 ? '正常' :'关闭';?></span>
                            <span style="width: 20%;float: left;display: inline-block;padding:0 5px;word-wrap:break-word"><?php echo !empty($value['create_time']) ? date('Y-m-d H:i:s',$value['create_time']) :'';?></span>
                            <span style="width: 20%;float: right;display: inline-block;padding:0 5px;word-wrap:break-word">
                                <?php if(Yii::$app->cache->get('admin')['role'] != 3){?>
                                    <input type="hidden" value="<?php echo $value['id']?>" id="user_id">
                                    <input type="hidden" value="<?php echo $value['status'] == 1 ? '1' :'2';?>" id="del_type">
                                    <a class="btn-xs btn-warning btn-primary demo1"><?php echo $value['status'] == 1 ? '关闭' :'开启';?></a>
                                    <a class="btn-xs btn-warning btn-primary demo2">删除</a>
                                <?php }?>
                            </span>
                        </li>
                        <?php }}?>
                    </ul>
                    <div class="row">
                        <div class="col-sm-12  form-inline text-right">
                            <div class="btn-group" align="center">
                                <?= LinkPager::widget(['pagination' => $pagination, 'options'=>['class'=>'page-num'],'firstPageLabel' =>'首页','lastPageLabel'=>'尾页']); ?>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    <!-- page end-->
    </section>
</section>
<div class="modal inmodal" id="myModal1" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
      <div class="modal-content animated bounceInRight">
          <div class="modal-header">
              <button type="button" class="close invoiceclose" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
              </button>
              <h4 class="modal-title">新增用户 </h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-3 control-label">*用户昵称：</label>
                    <div class="col-sm-6">
                      <input class="form-control" id="username" type="text"  placeholder="用户昵称不能为空">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">*用户电话：</label>
                    <div class="col-sm-6">
                      <input class="form-control" id="mobile" type="text"  placeholder="电话号码不能为空">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">*用户初始密码：</label>
                    <div class="col-sm-6">
                      <input class="form-control" id="password" type="text"  placeholder="用户初始密码不能为空">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">*用户角色：</label>
                    <div class="col-sm-6">
                        <select name="" id="role" class="btn btn-default">
                            <option value="">请选择</option>
                            <?php if(!empty(Yii::$app->params['role'])){foreach(Yii::$app->params['role'] as $k=>$v){?>
                            <option value="<?php echo $k?>"><?php echo $v['level']?></option>
                            <?php }}?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">真实姓名：</label>
                    <div class="col-sm-6">
                      <input class="form-control" id="name" type="text">
                    </div>
                </div>
            </div> 
          </div>
          <div class="modal-footer">
              <button type="button" class="btn btn-primary" id="add_user" data-dismiss="modal">确认</button>
          </div>
      </div>
  </div>
</div>

<script src="/js/plugins/sweetalert/sweetalert.min.js"></script>
<script>
$(".slecctform").click(function(){
    var str = "1=1";
    var username = $("#search_name").val();
    var mobile = $("#search_mobile").val();
    if(username.length >= 0){
        str += "&username="+username;
    }
    if(mobile.length >= 0){
        str += "&mobile="+mobile;
    }
    window.location.href='list?'+str;
});


$("#add_user").click(function(){
    var mobile = $("#mobile").val();
    var password = $("#password").val();
    var username = $("#username").val();
    var name = $("#name").val();
    var role = $("#role").val();
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(username.length <= 0){
        alert("用户名不能为空");
        return false;
    }
    if(!myreg.test(mobile)){ 
        alert('请输入有效的手机号码！'); 
        return false; 
    }
    if(password.length <= 0){
        alert("请设置初始密码");
        return false;
    }
    if(role.length <= 0){
        alert("请选择角色");
        return false;
    }
    $.ajax({
        type: "post",
        url: "add-ajax-do",
        data: {username:username,mobile:mobile,password:password,role:role,name:name},
        dataType: "json",
        success: function(param){
        if(param.code == '20000'){
            swal("新增成功！", param.message, "success");
            location.reload();
        }else{
            swal("新增失败！", param.message, "error")
        }
      }
    })
})

//删除确认
$('.demo1').click(function () {
    var id = $(this).siblings("#user_id").val();
    var type = $(this).siblings("#del_type").val();
    if(type == 1){
        $title = '关闭';
    }else if(type == 2){
        $title = '开启';
    }else{
        $title = '删除';
    }
    swal({
        title: "您确定要"+$title+"该帐号吗？",
        text: "该操作很重要，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: $title,
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false,
        closeOnCancel: false
    }, function (isConfirm) {
        if (isConfirm) {
            $.ajax({
                type: "post",
                    url: "dele-ajax-do",
                    data: {id:id,type:type},
                    dataType: "json",
                    success: function(param){
                    if(param.code == '20000'){
                        swal($title+"成功！", param.message, "success");
                        location.reload();
                    }else{
                        swal($title+"失败！", param.message, "error")
                    }
                  }
            });
        } else {
            swal("已取消", "您取消了"+$title+"操作！", "error")
        }
    });
});

//删除确认
$('.demo2').click(function () {
    var id = $(this).siblings("#user_id").val();
    $title = '删除';
    swal({
        title: "您确定要"+$title+"该帐号吗？",
        text: "该操作很重要，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: $title,
        cancelButtonText: "让我再考虑一下…",
        closeOnConfirm: false,
        closeOnCancel: false
    }, function (isConfirm) {
        if (isConfirm) {
            $.ajax({
                type: "post",
                    url: "dele-ajax-do",
                    data: {id:id},
                    dataType: "json",
                    success: function(param){
                    if(param.code == '20000'){
                        swal($title+"成功！", param.message, "success");
                        location.reload();
                    }else{
                        swal($title+"失败！", param.message, "error")
                    }
                  }
            });
        } else {
            swal("已取消", "您取消了"+$title+"操作！", "error")
        }
    });
});
</script>
